package com.blue.starter.sql.mp;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.blue.common.utils.UserContext;
//import com.blue.starter.interceptor.MvcConfig;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import java.util.Date;
/**
 * @Author : CaiYang
 * @Date : 2025-03-20 19:01
 * @Description :
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = new Date();
        String loginId = String.valueOf(UserContext.getUser());
        this.strictInsertFill(metaObject, "createTime", Date.class, now);
        this.strictInsertFill(metaObject, "createBy", String.class, loginId);
        this.strictUpdateFill(metaObject, "updateTime", Date.class, now);
        this.strictInsertFill(metaObject, "updateBy", String.class, loginId);
        // 默认设置状态1
        this.setFieldValByName("deleteStatus", 1, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date now = new Date();
        String loginId = String.valueOf(UserContext.getUser());
        // 更新时填充更新时间
        this.strictUpdateFill(metaObject, "updateTime", Date.class, now);
        this.strictInsertFill(metaObject, "updateBy", String.class, loginId);
    }
}
